package com.lauor.test.transaction;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.lauor.smpedr.param.SqlArgMap;
import com.lauor.smpedr.session.SqlSession;
import com.lauor.test.BaseSpringBootUser;
import com.lauor.test.entity.User;
import org.junit.Test;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 正常测试事务
 */
public class NormalTransactionUser extends BaseSpringBootUser {

    @Test
    @Rollback(false)//true:回滚，false：不回滚
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ)
    public void testTransaction() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //正常插入一条
        User user = this.buildUserMsg();
        int effectRows = sqlSession.insert(User.class, user);
        System.out.println( String.format("成功插入%s条", effectRows) );
        //查询插入成功的数据
        SqlArgMap<Integer> sqlArgMap = new SqlArgMap<>(4);
        sqlArgMap.put("id", user.getId());
        User userInDb = sqlSession.selectOne(User.class, sqlArgMap);
        System.out.println( String.format("插入的数据:%s", JSON.toJSONString(userInDb, SerializerFeature.WriteMapNullValue)) );
    }
}
